﻿Imports System.Data
Imports ADOEntity

Partial Class Administrator_frmNoLogOut
    Inherits Web.UI.BasePage
    Private Const strMenuCode As String = "M70"
    Dim userExpireEntity As New userEntities
    Dim dsUserExpire As New DataSet
    Dim clsDataTable As New clsLINQToDataTable
    Dim clsSessionMenuRight As clsSessionMenuRight
    Dim systemConfigEntity As New systemConfigEntities

    Dim expirePWDAdmin As Integer
    Dim expirePWDUser As Integer
    Dim expireDateUser As Integer
    Dim defaultPWD As String
    Dim clsFormatDate As clsDateFormat
    Dim clsMasterDropDown As clsMasterDropDown


#Region " Even Page "
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        btnSearch.Text = Resources.resButton.btnSearch
        btnLock.Text = Resources.resButton.btnModify
        lblMsg.Text = Resources.resMessageAlert.alertEmptyGrid
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        chkIsLogin()
        clsMasterDropDown = New clsMasterDropDown
        clsSessionMenuRight = Session("clsMenuRight")
        If (Not IsPostBack) Then
            LoadDataToDropdown()
            setUserRightButton()
            setDefaultSearch()
            chkIsLogin()
            CreateControlMP()
        Else

            dsUserExpire = Session("dsTblMaster")

        End If

    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("dsTblMaster") = dsUserExpire
    End Sub

#End Region

#Region "SetForm"

    Sub CreateControlMP()

        Dim lblForm As Label = CType(Master.FindControl("lblForm"), Label)
        lblForm.Text = "ปลดล๊อคสถานะล๊อคอินค้าง"
    End Sub

#End Region

#Region " Even Button "

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        IsDataBind()
        clearGvIndex()
    End Sub

    Protected Sub btnLock_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLock.Click
        If Not (gvUserExpire.Rows.Count = 0) Then
            userExpireEntity.Connection.Open()
            Using transaction As System.Data.Common.DbTransaction = userExpireEntity.Connection.BeginTransaction()
                Try
                    Dim chkSelect As CheckBox
                    For i As Integer = 0 To gvUserExpire.Rows.Count - 1
                        chkSelect = CType(gvUserExpire.Rows(i).FindControl("chkSelect"), CheckBox)
                        If chkSelect.Checked = True Then

                            savePWDLOCKDATE(gvUserExpire.Rows(i).Cells(2).Text)
                        
                        End If
                    Next

                    transaction.Commit()
                Catch ex As Exception
                    transaction.Rollback()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('Error : " & ex.ToString & " Error message : " & ex.Message & "')", True)
                End Try
            End Using

            userExpireEntity.SaveChanges()

            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('ปลดล็อคเรียบร้อยแล้ว')", True)

            IsDataBind()
        Else
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + Resources.resValidate.nonSelectGrid + "')", True)
        End If

    End Sub

#End Region

#Region "Even GridView"

    Protected Sub gvUserExpire_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvUserExpire.PageIndexChanging
        gvUserExpire.PageIndex = e.NewPageIndex
        gvUserExpire.DataSource = dsUserExpire.Tables("UserExpire")
        gvUserExpire.DataBind()

        clearGvIndex()
    End Sub
    Protected Sub gvUserExpire_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvUserExpire.Sorting
        Dim dt = TryCast(dsUserExpire.Tables("UserExpire"), DataTable)

        If dt IsNot Nothing Then

            'Sort the data.
            dt.DefaultView.Sort = e.SortExpression & " " & GetSortDirection(e.SortExpression)
            gvUserExpire.DataSource = dsUserExpire.Tables("UserExpire")
            gvUserExpire.DataBind()

            clearGvIndex()
        End If
    End Sub
    Protected Sub gvUserExpire_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvUserExpire.RowDataBound
        clsFormatDate = New clsDateFormat
        If (e.Row.RowType.ToString = "DataRow") Then
            If Not (e.Row.Cells(6).Text = Nothing) Then
                e.Row.Cells(6).Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, e.Row.Cells(6).Text)
            End If
        End If
    End Sub

    Protected Sub chkAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chk As CheckBox = CType(gvUserExpire.HeaderRow.FindControl("chkAll"), CheckBox)
        If (chk.Checked) Then
            For i As Int16 = 0 To gvUserExpire.Rows.Count - 1
                Dim chkrow As CheckBox = CType(gvUserExpire.Rows(i).FindControl("chkSelect"), CheckBox)
                chkrow.Checked = True

            Next
        Else

            For i As Int16 = 0 To gvUserExpire.Rows.Count - 1
                Dim chkrow As CheckBox = CType(gvUserExpire.Rows(i).FindControl("chkSelect"), CheckBox)
                chkrow.Checked = False

            Next

        End If

    End Sub


    Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        For i As Int16 = 0 To gvUserExpire.Rows.Count - 1
            Dim chkrow As CheckBox = CType(gvUserExpire.Rows(i).FindControl("chkSelect"), CheckBox)
            If chkrow.Checked = False Then
                Dim chk As CheckBox = CType(gvUserExpire.HeaderRow.FindControl("chkAll"), CheckBox)
                chk.Checked = False
                Exit For
            End If
        Next


    End Sub

#End Region

#Region " Function"

    Public Sub setDefaultSearch()
        clsFormatDate = New clsDateFormat
        txtStartDate.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
        txtEndDate.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)



    End Sub
    Private Sub setUserRightButton()
        Dim col As New Collection
        col.Add(btnSearch, statusbtn.Query.ToString)
        col.Add(btnLock, statusbtn.Modify1.ToString)
        col.Add(btnPrint, statusbtn.print.ToString)
        setBtnToUserRight(col, strMenuCode, clsSessionMenuRight.getDsMenuRight)

    End Sub
    Private Sub LoadDataToDropdown()
        Dim clsSessionMenuRight As clsSessionMenuRight
        clsSessionMenuRight = Session("ClsMenuRight")

        'clsMasterDropDown.setUserRightToDdlSearch(ddlUserRight)

        clsMasterDropDown.setDeptToDdlSearch(ddlDepartMent, _
                                         strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                         strRoleCode:=clsSessionMenuRight.getRoleGroup)

        clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartMent.SelectedValue, _
                                                   strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                             strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                             strZoneCode:=clsSessionMenuRight.getZoneCode)


    End Sub

    Private Sub clearGvIndex()
        gvUserExpire.SelectedIndex = -1


    End Sub

    Private Sub IsDataBind()
        Dim clsSessionMenuRight As New clsSessionMenuRight
        Dim clsFormatDate As New clsDateFormat
        clsSessionMenuRight = Session("ClsMenuRight")

        If dsUserExpire.Tables.Contains("UserExpire") Then
            dsUserExpire.Tables.Remove("UserExpire")
        End If

        Dim qryUserExpire = From pwdExpire In userExpireEntity.SUSER Join Branch In userExpireEntity.SBRANCH On pwdExpire.BRANCHCODE Equals Branch.BRANCHCODE Join dept In userExpireEntity.SDEPARTMENT On pwdExpire.DEPTCODE Equals dept.DEPTCODE _
                           Where pwdExpire.USERNAME <> clsSessionMenuRight.getUserName _
                           Order By pwdExpire.USERNAME _
                           Select pwdExpire.USERNAME, EmpFullName = pwdExpire.EMPNAME & " " & pwdExpire.EMPLASTNAME, _
                           pwdExpire.EMPNAME, pwdExpire.EMPLASTNAME, Branch.BRANCHNAME, dept.DEPTNAME, _
         pwdExpire.LASTLOGINDATE, pwdExpire.ISLOGIN,
        pwdExpire.DEPTCODE, pwdExpire.ROLECODE, pwdExpire.BRANCHCODE, pwdExpire.ZONECODE, pwdExpire.USERRIGHT


        If (clsSessionMenuRight.getRoleGroup > 89) Then
        ElseIf (clsSessionMenuRight.getRoleGroup > 79) Then
            qryUserExpire = qryUserExpire.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
        ElseIf (clsSessionMenuRight.getRoleGroup > 69) Then
            qryUserExpire = qryUserExpire.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
        ElseIf (clsSessionMenuRight.getRoleGroup > 59) Then
            qryUserExpire = qryUserExpire.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup And fn.ZONECODE = clsSessionMenuRight.getZoneCode)
        ElseIf (clsSessionMenuRight.getRoleGroup < 60) Then
            qryUserExpire = qryUserExpire.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
        End If



        Dim dteDateNow As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, dteDateIsChk:=Date.Now)
        qryUserExpire = qryUserExpire.Where(Function(pwdLock) pwdLock.ISLOGIN = True And pwdLock.USERNAME <> clsSessionMenuRight.getUserName)

        If Not (txtStartDate.Text = "" And txtEndDate.Text = "") Then
            Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtStartDate.Text)
            Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text)
            qryUserExpire = qryUserExpire.Where(Function(tbl_Amlo) tbl_Amlo.LASTLOGINDATE >= strStartDate And tbl_Amlo.LASTLOGINDATE <= strEndDate)
        End If




        If Not (txtUserName.Text = "") Then
            qryUserExpire = qryUserExpire.Where(Function(pwdLock) pwdLock.USERNAME.Contains(txtUserName.Text))
        End If

        If Not (txtFirstName.Text = "") Then
            qryUserExpire = qryUserExpire.Where(Function(pwdLock) pwdLock.EMPNAME.Contains(txtFirstName.Text))
        End If

        If Not (txtLastName.Text = "") Then
            qryUserExpire = qryUserExpire.Where(Function(pwdLock) pwdLock.EMPLASTNAME.Contains(txtLastName.Text))
        End If
       
        'If Not (ddlUserRight.SelectedValue = "0") Then
        '    qryUserExpire = qryUserExpire.Where(Function(LogLogin) LogLogin.USERRIGHT = ddlUserRight.SelectedValue)
        'End If

        If Not (ddlDepartMent.SelectedValue = "0") Then
            qryUserExpire = qryUserExpire.Where(Function(LogLogin) LogLogin.DEPTCODE = ddlDepartMent.SelectedValue)
        End If
        If Not (ddlBranch.SelectedValue = "0") Then
            qryUserExpire = qryUserExpire.Where(Function(LogLogin) LogLogin.BRANCHCODE = ddlBranch.SelectedValue)
        End If

        dsUserExpire.Tables.Add(clsDataTable.LINQToDataTable(qryUserExpire, "UserExpire").Copy)

        If (qryUserExpire.Count = 0) Then
            lblMsg.Visible = True
        Else
            lblMsg.Visible = False

        End If

        gvUserExpire.DataSource = dsUserExpire.Tables("UserExpire")
        gvUserExpire.DataBind()
        clsDataTable = Nothing
    End Sub

    Private Function GetSortDirection(ByVal column As String) As String

        ' By default, set the sort direction to ascending.
        Dim sortDirection = "DESC"

        ' Retrieve the last column that was sorted.
        Dim sortExpression = TryCast(ViewState("SortExpression"), String)

        If sortExpression IsNot Nothing Then
            ' Check if the same column is being sorted.
            ' Otherwise, the default value can be returned.
            If sortExpression = column Then
                Dim lastDirection = TryCast(ViewState("SortDirection"), String)
                If lastDirection IsNot Nothing _
                  AndAlso lastDirection = "DESC" Then

                    sortDirection = "ASC"

                End If
            End If
        End If

        ' Save new values in ViewState.
        ViewState("SortDirection") = sortDirection
        ViewState("SortExpression") = column
        Return sortDirection
    End Function

    Private Sub setConfig()
        Dim qry = (From systemConfig In systemConfigEntity.SSYSTEMCONFIG
                             Order By systemConfig.CONFIGINDEX).ToList

        expirePWDAdmin = qry(0).CONFIGNUM
        expirePWDUser = qry(1).CONFIGNUM
        expireDateUser = qry(6).CONFIGNUM
        defaultPWD = qry(7).CONFIGSTR

        qry = Nothing

    End Sub

    Private Sub savePWDLOCKDATE(ByVal username As String) 'login เกิน
        Dim updateUser = (From u In userExpireEntity.SUSER
                        Where u.USERNAME = username
                        Select u).First()

        updateUser.ISLOGIN = 0

    End Sub



#End Region

    Protected Sub ddlDepartMent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepartMent.SelectedIndexChanged
        Dim clsDropDown As New clsMasterDropDown
        'clsSessionMenuRight = Session("ClsMenuRight")
        If Not (ddlDepartMent.SelectedValue = "0") Then
            clsDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartMent.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
        Else
            ddlBranch.SelectedValue = "0"
        End If

        clsDropDown = Nothing

    End Sub

    
  
    Protected Sub gvUserExpire_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvUserExpire.SelectedIndexChanged

    End Sub

 
End Class
